package org.mspring.mlog.service;

import java.util.Date;

import org.mspring.mlog.dao.LogDao;
import org.mspring.mlog.entity.Log;
import org.mspring.platform.dao.Page;
import org.mspring.platform.dao.query.QueryBuilder;
import org.mspring.platform.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author Gao Youbo
 * @since 2013-06-18 16:34:42
 */
@Service
@Transactional
public class LogService {

    @Autowired
    private LogDao logDao;

    public Log get(long id) {
        return logDao.get(id);
    }

    public Page<Log> listPage(QueryBuilder queryBuilder, Page<Log> page) {
        return logDao.listPage(queryBuilder, page);
    }

    /**
     * @param days
     */

    public void deleteLogs(int days) {
        if (days > 0) {
            Date date = DateUtils.addDays(new Date(), -days);
            String dateStr = DateUtils.format(date, DateUtils.YYYY_MM_DD_HH_MM_SS);
            logDao.clearBeforeTime(dateStr);
        }
        logDao.clear();
    }

    /**
     * 创建日志
     *
     * @param log
     * @return
     */

    public Long insert(Log log) {
        if (log.getActionTime() == null) {
            log.setActionTime(new Date());
        }
        return logDao.insert(log);
    }

}
